package cn.pid21.client.util;

import cn.pid21.client.exception.DataReportClientException;
import okhttp3.MediaType;
import okhttp3.RequestBody;

import java.io.File;

/**
 * RequestBody 工具类,用于创建 json/xml 数据格式请求
 *
 * @author 郭志斌
 * @version 1.0.0
 * <b>Creation Time:</b> 2019/8/14 14:21
 */
public final class RequestBodyUtil {

    public static final String APPLICATION_JSON_UTF8 = "application/json;charset=UTF-8";
    public static final String APPLICATION_XML_UTF8 = "application/xml;charset=UTF-8";

    private RequestBodyUtil(){}

    /**
     * json 格式数据 RequestBody
     *
     * @param param json
     * @return RequestBody
     */
    public static RequestBody json(String param){
        MediaType json = MediaType.parse(APPLICATION_JSON_UTF8);
        return RequestBody.create(json,param);
    }

    /**
     * xml 格式数据 RequestBody
     *
     * @param param xml
     * @return RequestBody
     */
    public static RequestBody xml(String param){
        MediaType xml = MediaType.parse(APPLICATION_XML_UTF8);
        return RequestBody.create(xml,param);
    }

    /**
     * 通过文件路径构建 XML 格式 RequestBody
     *
     * @param xmlFilePath
     * @return RequestBody
     */
    @Deprecated
    public static RequestBody xmlFilePath(String xmlFilePath){
       return null;
    }

    /**
     * 通过文件构建 XML 格式 RequestBody
     *
     * @param xmlFile
     * @return RequestBody
     */
    @Deprecated
    public static RequestBody xmlFile(File xmlFile){
        if(xmlFile == null || !xmlFile.isFile()){
            throw new DataReportClientException("无效的 XML 文件,请检查文件路径是否正确");
        }
        //解析 xml 文件
        String xmlParamString = "";
        return xml(xmlParamString);
    }
}
